package com.glr.question.controller;

import com.glr.common.contants.CodeEnum;
import com.glr.common.utils.ResultUtils;
import com.glr.question.entity.Question;
import com.glr.question.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 试题控制层
 *
 * @author GLoRy
 * @date 2021/6/11 0:48
 */
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 添加试题信息
     *
     * @param question 试题实体
     * @return 添加结果
     */
    @PostMapping("/save")
    public ResultUtils save(@RequestBody Question question) {
        questionService.save(question);
        return new ResultUtils(true, CodeEnum.SUCCEED, "添加试题信息成功");
    }

    /**
     * 根据id修改试题信息
     *
     * @param question 试题实体
     * @param id       试题id
     * @return 修改结果
     */
    @PutMapping("/update/{id}")
    public ResultUtils update(@RequestBody Question question, @PathVariable String id) {
        question.setQuestionId(Integer.valueOf(id));
        questionService.update(question);
        return new ResultUtils(true, CodeEnum.SUCCEED, "修改试题信息成功");
    }

    /**
     * 根据id删除试题信息
     *
     * @param id 试题id
     * @return 删除信息
     */
    @DeleteMapping("/delete/{id}")
    public ResultUtils delete(@PathVariable String id) {
        questionService.delete(Integer.valueOf(id));
        return new ResultUtils(true, CodeEnum.SUCCEED, "修改试题信息成功");
    }

    /**
     * 根据id查询试题信息
     *
     * @param id 试题id
     * @return 根据id查询的课程结果
     */
    @GetMapping("/findById/{id}")
    public ResultUtils findById(@PathVariable String id) {
        String question = redisTemplate.opsForValue().get("question::" + id);
        if (question != null) {
            return new ResultUtils(true, CodeEnum.SUCCEED, "查询试题信息成功", question);
        } else {
            return new ResultUtils(true, CodeEnum.SUCCEED, "查询试题信息成功", questionService.findById(Integer.valueOf(id)));
        }
    }

    /**
     * 查询所有试题信息
     *
     * @return 查询试题信息的结果
     */
    @GetMapping("/findAll")
    public ResultUtils findAll() {
        String questionList = redisTemplate.opsForValue().get("question::findAll");
        if (questionList != null) {
            return new ResultUtils(true, CodeEnum.SUCCEED, "查询试题信息成功", questionList);
        } else {
            return new ResultUtils(true, CodeEnum.SUCCEED, "查询试题信息成功", questionService.findAll());
        }
    }

    /**
     * 分页查询所有试题信息
     *
     * @param page     当前页
     * @param pageSize 每页条数
     * @return 分页查询试题的信息的结果
     */
    @GetMapping("/findByPage/{page}/{pageSize}")
    public ResultUtils findByPage(@PathVariable String page, @PathVariable String pageSize) {
        String questionPage = redisTemplate.opsForValue().get("question::findByPage");
        if (questionPage != null) {
            return new ResultUtils(true, CodeEnum.SUCCEED, "查询试题信息成功", questionPage);
        } else {
            Page<Question> questions = questionService.findByPage(Integer.valueOf(page), Integer.valueOf(pageSize));
            return new ResultUtils(true, CodeEnum.SUCCEED, "查询试题信息成功", questions.getContent());
        }
    }
}
